package com.lesso.cyg.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.cyg.sunflower.core.ApiResponse;
import com.lesso.cyg.sunflower.pojo.dto.DeviceDTO;
import com.lesso.cyg.sunflower.pojo.dto.DeviceQueryDTO;
import com.lesso.cyg.sunflower.pojo.vo.DeviceVO;
import com.lesso.cyg.sunflower.service.IDeviceService;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 设备基础信息 前端控制器
 * </p>
 *
 * @author cyg
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/base/device")
public class DeviceController {
    @DubboReference
    private IDeviceService deviceService;

    @PostMapping
    public ApiResponse<Integer> save(@Valid @RequestBody DeviceDTO dto) {
        return ApiResponse.ok("success", deviceService.save(dto));
    }

    @GetMapping("/page")
    public ApiResponse<Page<DeviceVO>> page(DeviceQueryDTO dto) {
        return ApiResponse.ok("success", deviceService.page(dto));
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<DeviceVO>> customPage(DeviceQueryDTO dto) {
        return ApiResponse.ok("success", deviceService.customPage(dto));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Object> delete(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能为负数");
        }
        deviceService.deleteById(id);
        return ApiResponse.ok();
    }

    @PutMapping
    public ApiResponse<Integer> update(@Valid @RequestBody DeviceDTO dto) {
        return ApiResponse.ok("success", deviceService.update(dto));
    }
}